package com.ruoyi.supplier.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supplier.domain.*;
import com.ruoyi.supplier.mapper.TbProductSpecItemMapper;
import com.ruoyi.supplier.mapper.TbProductSpecMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.mapper.TbPriceMapper;
import com.ruoyi.supplier.service.ITbPriceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 价格Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-02-24
 */
@Service
public class TbPriceServiceImpl implements ITbPriceService 
{
    public static final Logger logger = LoggerFactory.getLogger(TbPriceServiceImpl.class);

    @Autowired
    private TbPriceMapper tbPriceMapper;

    @Autowired
    private TbProductSpecMapper tbProductSpecMapper;

    @Autowired
    private TbProductSpecItemMapper tbProductSpecItemMapper;

    /**
     * 查询价格
     * 
     * @param id 价格ID
     * @return 价格
     */
    @Override
    public TbPrice selectTbPriceById(Long id)
    {
        return tbPriceMapper.selectTbPriceById(id);
    }

    /**
     * 查询价格列表
     * 
     * @param tbPrice 价格
     * @return 价格
     */
    @Override
    public List<TbPrice> selectTbPriceList(TbPrice tbPrice)
    {
        return tbPriceMapper.selectTbPriceList(tbPrice);
    }

    /**
     * 查询价格列表
     *
     * @param tbPrice 价格
     * @return 价格
     */
    @Override
    public List<TbPrice> selectTbPriceListProduct(TbPrice tbPrice)
    {
        return tbPriceMapper.selectTbPriceListProduct(tbPrice);
    }

    /**
     * 新增价格
     * 
     * @param tbPrice 价格
     * @return 结果
     */
    @Override
    public int insertTbPrice(TbPrice tbPrice)
    {
        tbPrice.setCreateTime(DateUtils.getNowDate());
        tbPrice.setCreateBy(SecurityUtils.getUsername());
        return tbPriceMapper.insertTbPrice(tbPrice);
    }

    /**
     * 修改价格
     * 
     * @param tbPrice 价格
     * @return 结果
     */
    @Override
    public int updateTbPrice(TbPrice tbPrice)
    {
        tbPrice.setUpdateTime(DateUtils.getNowDate());
        tbPrice.setUpdateBy(SecurityUtils.getUsername());
        return tbPriceMapper.updateTbPrice(tbPrice);
    }

    @Override
    public int updateTbPrice(List<TbPrice> list) {
        int i=0;
        for (TbPrice tbPrice: list) {
            tbPrice.setUpdateTime(DateUtils.getNowDate());
            tbPrice.setUpdateBy(SecurityUtils.getUsername());
            i+=tbPriceMapper.updateTbPrice(tbPrice);
        }
        return i;
    }

    /**
     * 批量删除价格
     * 
     * @param ids 需要删除的价格ID
     * @return 结果
     */
    @Override
    public int deleteTbPriceByIds(Long[] ids)
    {
        return tbPriceMapper.deleteTbPriceByIds(ids);
    }

    /**
     * 删除价格信息
     * 
     * @param id 价格ID
     * @return 结果
     */
    @Override
    public int deleteTbPriceById(Long id)
    {
        return tbPriceMapper.deleteTbPriceById(id);
    }

    @Override
    public int updateTbPriceAddQty(TbPrice tbPrice) {
        tbPrice.setUpdateTime(DateUtils.getNowDate());
        return tbPriceMapper.updateTbPriceAddQty(tbPrice);
    }

    @Override
    public int updateTbPriceReduceQty(TbPrice tbPrice) {
        tbPrice.setUpdateTime(DateUtils.getNowDate());
        return tbPriceMapper.updateTbPriceReduceQty(tbPrice);
    }

    @Override
    @Transactional
    public int updateTbPriceSet(TbProduct tbProduct) {

        List<TbProductSpec> tbProductSpecs = tbProductSpecMapper.selectTbProductSpecByTypeId(tbProduct.getTypeId());
        List list=new ArrayList();
        tbProductSpecs.forEach(tbProductSpec -> {
            List<TbProductSpecItem> tbProductSpecItems = tbProductSpecItemMapper.selectTbProductSpecItemBySpecId(tbProductSpec.getId());
            List listData=new ArrayList();
            tbProductSpecItems.forEach( tbProductSpecItem -> {
                listData.add(tbProductSpecItem.getItemName());
            });

            list.add(listData);
        });


        List<List<String>> listName = getDescartes(list);

        int i = tbPriceMapper.selectTbPriceByProductId(tbProduct.getId());
        if(i == listName.size()){
            return 0;
        }

        tbPriceMapper.deleteTbPriceByProducId(tbProduct.getId());

        listName.forEach(str -> {
            TbPrice tbPrice=new TbPrice();
            tbPrice.setTypeId(tbProduct.getTypeId());
            // TODO 先要插入商品表，返回ID
            tbPrice.setProductId(tbProduct.getId());
            tbPrice.setProductName(tbProduct.getProductName());
            tbPrice.setItemName(str.toString());
            tbPrice.setProductSn(tbProduct.getProductSn());
            tbPrice.setBrandId(tbProduct.getBrandId());
            tbPrice.setOrigin(tbProduct.getOrigin());
            tbPrice.setMeasurement(tbProduct.getMeasurement());
            tbPrice.setShelfLife(tbProduct.getShelfLife());
            tbPrice.setShelfLifeType(tbProduct.getShelfLifeType());
            tbPrice.setProductSort(tbProduct.getProductSort());
            tbPrice.setStoreCount(0); //初始化库存为零

            tbPriceMapper.insertTbPrice(tbPrice);
        });

        return listName.size();
    }

    private  <T> List<List<T>> getDescartes(List<List<T>> list) {
        List<List<T>> returnList = new ArrayList<>();
        descartesRecursive(list, 0, returnList, new ArrayList<T>());
        return returnList;
    }

    /**
     * 递归实现
     * 原理：从原始list的0开始依次遍历到最后
     *
     * @param originalList 原始list
     * @param position     当前递归在原始list的position
     * @param returnList   返回结果
     * @param cacheList    临时保存的list
     */
    private  <T> void descartesRecursive(List<List<T>> originalList, int position, List<List<T>> returnList, List<T> cacheList) {
        List<T> originalItemList = originalList.get(position);
        for (int i = 0; i < originalItemList.size(); i++) {
            //最后一个复用cacheList，节省内存
            List<T> childCacheList = (i == originalItemList.size() - 1) ? cacheList : new ArrayList<>(cacheList);
            childCacheList.add(originalItemList.get(i));
            if (position == originalList.size() - 1) {//遍历到最后退出递归
                returnList.add(childCacheList);
                continue;
            }
            descartesRecursive(originalList, position + 1, returnList, childCacheList);
        }
    }
}
